/*

   highlight v1

   Highlights arbitrary terms.

   <http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html>

   MIT license.

   Johann Burkard
   <http://johannburkard.de>
   <mailto:jb@eaio.com>

 */

jQuery.highlight =  function(sel) {
	pNodes = findCommonParent(sel.anchorNode, sel.focusNode);
	
	console.debug(pNodes[0].textContent + "\n\n ---\n\n " + pNodes[1].textContent);
	walkBetween(pNodes[0], pNodes[1], convert);
	walkRightUp(sel.anchorNode, pNodes[0], convert);
	walkLeftUp(sel.focusNode, pNodes[1], convert);
	bubbleUp(sel.anchorNode, unhighlight);
	bubbleUp(sel.focusNode, unhighlight);
	
	function findCommonParent(node1, node2) {
		node1parents = $(node1).parents();
		node2parents = $(node2).parents();
		for (var i = 0; i < node1parents.length; i++) {
			for (var j = 0; j < node2parents.length; j++) {
				if (node1parents[i] == node2parents[j]) return [node1parents[i-1],node2parents[j-1]];
			}
		}

	}
	
	function walkBetween(nodeA, nodeB, func) {
		if (nodeA == nodeB) return;
		if (nodeA.nextSibling) walkBetween(nodeA.nextSibling, nodeB, func);
		func(nodeA);
	}

	function walkRightUp(node, end, func) {
		if (node.nextSibling) {
			walkRightUp(node.nextSibling, end, func);
		} else if (node.parentNode != end) {
			//console.warn("it went up");
			walkRightUp(node.parentNode, end, func); }
		func(node);
		
	}
	
	function walkLeftUp(node, end, func) {
		if (node.prevSibling) {
			walkLeftUp(node.prevSibling, end, func);
		} else if (node.parentNode != end) {
			//console.warn("it went up");
			walkLeftUp(node.parentNode, end, func); }
		func (node);
	}

	function bubbleUp(node, func) {
		if (node.parentNode) bubbleUp(node.parentNode, func);
		func(node);

	}

	function unhighlight(node) {
		if (node.nodeType == 1) $(node).removeClass("highlight");

	}

    function convert(el){
    	console.debug('converting >> ' + el + el.textContent);
	if (el.nodeType == 3) {
	    span = document.createElement('span');
	    span.className = 'highlight';
	    elclone = el.cloneNode(true);
	    span.appendChild(elclone);
	    el.parentNode.replaceChild(span, el);

	}
	else if (el.nodeType == 1) {
	    $(el).addClass("highlight");
	    //el.className = el.className + " " + "highlight";
	    //console.error(el.className);

	}
	else alert("wierd node type: " + el);
    };

    /*
       var pos, skip, spannode, middlebit, endbit, middleclone;
       skip = 0;
       if (node.nodeType == 3) {
       pos = node.data.toUpperCase().indexOf(te);
       if (pos >= 0) {
       spannode = document.createElement('span');
       spannode.className = 'highlight';
       middlebit = node.splitText(pos);
       endbit = middlebit.splitText(te.length);
       middleclone = middlebit.cloneNode(true);
       spannode.appendChild(middleclone);
       middlebit.parentNode.replaceChild(spannode, middlebit);
       skip = 1;
       }
       }
       else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
       for (var i = 0; i < node.childNodes.length; ++i) {
       i += $.highlight(node.childNodes[i], te);
       }
       }
       return skip;
     */
};
/*
   jQuery.fn.removeHighlight = function() {
   return this.find("span.highlight").each(function() {
   this.parentNode.replaceChild(this.firstChild, this).normalize();
   });
   };
 */
